package org.jeecg.modules.demo.hd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.demo.hd.entity.AppUserCashMain;
import org.jeecg.modules.demo.hd.mapper.AppUserCashMainMapper;
import org.jeecg.modules.demo.hd.service.IAppUserCashDetailService;
import org.jeecg.modules.demo.hd.service.IAppUserCashMainService;
import org.jeecg.modules.demo.hd.vo.UserCashMainSearchInputVo;
import org.jeecg.modules.demo.hd.vo.UserCashMainViewVo;
import org.jeecg.modules.demo.hd.vo.myProject.CashOutVo;
import org.jeecg.modules.demo.hd.vo.myProject.UserCashDetailVo;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;


/**
 * @Description: app_user_cash_main
 * @Author: jeecg-boot
 * @Date: 2025-02-04
 * @Version: V1.0
 */
@Service
public class AppUserCashMainServiceImpl extends ServiceImpl<AppUserCashMainMapper, AppUserCashMain> implements IAppUserCashMainService {

    @Resource
    private IAppUserCashDetailService userCashDetailService;

    @Override
    public Float queryCurrentCash() {
        LoginUser currentUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        String userId = currentUser.getId();
        QueryWrapper<AppUserCashMain> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        AppUserCashMain appUserCashMain = this.baseMapper.selectOne(queryWrapper);
        if (appUserCashMain != null) {
            return Float.parseFloat(appUserCashMain.getCashNum().toString());
        }
        return 0f;
    }

    @Override
    public String CashOut(CashOutVo vo) {
        // 查询用户现金余额是否够用
        AppUserCashMain userCashMain = getUserCashMain(vo.getUserId());
        if (userCashMain == null) {
            return "未找到用户信息！";
        } else {
            double goldNum1 = userCashMain.getCashNum() - vo.getCashOutNum();
            if (userCashMain.getCashNum() >= vo.getCashOutNum()) {
                // 创建用户现金余额操作明细
                UserCashDetailVo cashDetailVo = new UserCashDetailVo();
                cashDetailVo.setUserId(vo.getUserId());
                cashDetailVo.setCashNum(-vo.getCashOutNum());
                cashDetailVo.setCashStatus("1");
                cashDetailVo.setName("用户提现");
                cashDetailVo.setCashType("2");
                userCashDetailService.addDetail(cashDetailVo);
                // 更新用户现金总表
                userCashMain.setCashNum(goldNum1);
                this.baseMapper.updateById(userCashMain);
                return "提现成功！";
            }
            return "未找到用户信息！";
        }
    }

    @Override
    public IPage<UserCashMainViewVo> getList(UserCashMainSearchInputVo vo, IPage page) {
        List<UserCashMainViewVo> list = baseMapper.getUserCashMainList(page,vo);
        Integer total = baseMapper.getUserCashMainTotal(vo);
        IPage<UserCashMainViewVo> result = new Page<>(page.getCurrent(), page.getSize(), total);
        result.setRecords(list);
        return result;
    }

    @Override
    @Transactional
    public Boolean updateCashMain(String userId, double cashNum) {
        // 获取用户金币主表实体
        AppUserCashMain userCashMain = getUserCashMain(userId);
        if (userCashMain == null) {
            userCashMain = new AppUserCashMain();
            userCashMain.setUserId(userId);
            userCashMain.setCashNum(cashNum);
            userCashMain.setBonusTime(DateUtils.getDate());
            this.baseMapper.insert(userCashMain);
        } else {
            double goldNum1 = userCashMain.getCashNum();
            userCashMain.setCashNum(goldNum1 + cashNum);
            this.baseMapper.updateById(userCashMain);
        }
        return true;
    }

    private AppUserCashMain getUserCashMain(String userId) {
        LambdaQueryWrapper<AppUserCashMain> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(AppUserCashMain::getUserId, userId);
        AppUserCashMain userCashMain = this.baseMapper.selectOne(queryWrapper);
        return userCashMain;
    }
}
